#include <string.h>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;

class Person {
public:
    Person(const char*name,int age)
        :_name(new char[strlen(name)+1]())
         ,_age(age) 
    {
       // cout<<"Person(const char*,int)"<<endl;
        strcpy(_name,name);
    }

    Person(const Person &rhs)
    :_name(new char[strlen(rhs._name)+1]())
    ,_age(rhs._age) 
    {
    strcpy(_name,rhs._name);
    //cout<<"Person(const Person&)"<<endl;
    }

    Person &operator=(const Person &rhs)
    {
        if(this !=&rhs){
            delete []_name;
            _name = new char[strlen(rhs._name)+1]();
            strcpy(_name,rhs._name);
            _age=rhs._age;
           // cout<<"Person &operator=(const Person&rhs)"<<endl;
        }
        return *this;
    }

    ~Person(){
        if(_name){
         //   cout<<"~Person"<<endl;
            delete []_name;
        }
    }

    void display(){
        cout<<"name: "<<_name<<"::"<<"age: "<<_age<<endl;
    }

private:
    char *_name;
    int _age;
};


class Employee
:public Person
{
public:
    Employee(const char*name,int age,const char*department,float salary)
        :Person(name,age)
         ,_department(new char[strlen(department)+1]())
         , _salary(salary)
    {
        strcpy(_department,department);
       // cout<<"Employee(const char *,int,const char*,float)"<<endl;
    }

    Employee(const Employee &rhs)
        :Person(rhs) 
         ,_department(new char[strlen(rhs._department)+1]())
         ,_salary(rhs._salary) 
    {
        strcpy(_department,rhs._department);
       // cout<<"Employee(const Employee &rhs)"<<endl;
    }

    Employee &operator=(const Employee &rhs)
    {
        if(this !=&rhs){
            Person::operator=(rhs);
            delete[]_department;
            _department = new char[strlen(rhs._department)+1]();
            strcpy(_department,rhs._department);
            _salary = rhs._salary;
        }
        return *this;
    }

    ~Employee(){
        if(_department){
            delete []_department;
        }
    }

    void display(){
        Person::display();
        cout<<"department: "<<_department<<"::"<<"salary"<<_salary<<endl;
    }

    float getSalary()const{
        return _salary;
    }

private:
    char *_department;
    float _salary;
};

float calc_average_salary(const vector<Employee>&employees){
    if(employees.empty()){
        return 0.0f;
    }

    float total = 0.0f;
    for(auto &it:employees){
        total +=it.getSalary();
    }
    return total/employees.size(); 
}


void test(){
    Employee p1("zs",21,"sale",3000);
    p1.display();
    Employee p2("ls",32,"develop",18000);
    p2.display();
    Employee p3(p2);
    p3.display();
    p3=p1;
    p3.display();
}
void test2(){
    vector<Employee>employees{
    Employee ("zs",21,"sale",3000),
    Employee ("ls",32,"develop",18000),
    Employee ("wu",27,"develop",15500)
    };
    for(auto &it:employees){
    it.display();
    }
    cout<<"the average salary: "<<calc_average_salary(employees)<<endl;

}

int main()
{

    test();
    test2 ();
    return 0;
}

